package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.ApprovalActivityBinding;
import com.sgb.capital.model.Approval2Bean;
import com.sgb.capital.model.BusinessInfoBean;
import com.sgb.capital.model.OrganizationBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.SelPop;
import com.sgb.capital.view.pop.TipPop;
import com.sgb.capital.viewmodel.ApprovalModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

/**
 * 作者:张磊
 * 日期:2021/10/28 0028
 * 说明:审批申请
 */
public class ApprovalActivity extends BaseActivity {
    private ApprovalActivityBinding mBinding;
    private ApprovalModel mModel;
    private SelPop mListPop;
    private TipPop mTipPop;
    private Approval2Bean mBean;
    private List<OrganizationBean.ChildrenDTO> mChildren;
    private String mS;
    private String mOrderId;

    public static void start(Context context, String name, int orderType, String data) {
        Intent intent = new Intent(context, ApprovalActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("orderType", orderType);
        intent.putExtra("data", data);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, String name, String orderId, int orderType) {
        Intent intent = new Intent(context, ApprovalActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("orderType", orderType);
        intent.putExtra("orderId", orderId);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.approval_activity);
        mTipPop = new TipPop(this);
        mListPop = new SelPop(this);
        mListPop.mTvTitleTip.setText("申请部门");
        mModel = new ViewModelProvider(this).get(ApprovalModel.class);
    }

    public void initData() {
        EventBus.getDefault().register(this);
        mOrderId = getIntent().getStringExtra("orderId");
        mModel.getBusinessInfoByCompany(this);
    }

    public void initListener() {
        mTipPop.mTvCancel.setOnClickListener(v -> {
            mTipPop.dismiss();
            if (mTipPop.mTip.contains("异常")) {
                startActivity(new Intent(this, OrderErrorActivity.class));
            } else if (mTipPop.mTip.contains("变化")) {
                Utils.sendMsg(Constants.EVENT_PAY_SUCCESS_PAY, null);
                finish();
            }
        });
        mBinding.tvBtn.setOnClickListener(v -> {
            mBean.remark = mBinding.etContent.getText().toString();
            if (Utils.isEmpty(mOrderId)) {
                mModel.apply(this, new Gson().fromJson(getIntent().getStringExtra("data"), BusinessInfoBean.class));
            } else {
                mModel.applyProcess(this, mBean);
            }
        });

        mBinding.llSelect.setOnClickListener(v -> mListPop.show(0, mBinding.view));
        mBinding.ivBack.setOnClickListener(v -> finish());
    }

    @Override
    public void initObserve() {
        mModel.mSubMerName.observe(this, s -> {
            mS = s;
            mModel.getOrganization(this);
        });
        mModel.mCode.observe(this, s -> {
            mBean.orderId = s;
            mModel.applyProcess(this, mBean);
        });
        mModel.mOrganizationBean.observe(this, organizationBean -> {
            if (organizationBean != null && organizationBean.size() != 0) {
                OrganizationBean item = organizationBean.get(0);
                mBinding.tvCompanyName.setText(mS);
                mBinding.tvPerson.setText(getIntent().getStringExtra("name"));
                mBean = new Approval2Bean();
                mBean.applierName = getIntent().getStringExtra("name");
                mBean.companyName = mS;
                if (mOrderId != null) {
                    mBean.orderId = mOrderId;
                }
                mBean.orderType = getIntent().getIntExtra("orderType", 1) == 1 ? 1 : 2;
                mChildren = item.children;
                List<String> mDatas = new ArrayList<>();
                for (OrganizationBean.ChildrenDTO data : mChildren) {
                    mDatas.add(data.name);
                }
                if (mDatas.size() != 0) {
                    mListPop.mTip = mDatas.get(0);
                }
                mListPop.mList = mDatas;
            }
        });

        mModel.mState.observe(this, s -> {
            if (Utils.isEmpty(s)) {
                if (Utils.isEmpty(mOrderId)) {
                    Utils.sendMsg(Constants.EVENT_REFRESH, null);
                } else {
                    Utils.sendMsg(Constants.EVENT_PAY_SUCCESS_PAY, null);
                }
                finish();
                return;
            }
            if (s.equals("E80810003") || s.equals("E80810004")) {
                mTipPop.initStr("审批状态已发生变化，请刷新页面后再操作", null, "知道了");
            } else {
                mTipPop.initStr("订单异常，无法付款", null, "知道了");
            }
            mTipPop.show();
        });
    }


    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_SELECT:
                if (mListPop.mTip == null) return;
                mBinding.tvSelect.setText(mListPop.mTip);
                mBean.departmentName = mListPop.mTip;
                mBinding.tvSelect.setTextColor(Utils.getColor(R.color.color_333333));
                for (OrganizationBean.ChildrenDTO data : mChildren) {
                    if (data.name.contains(mListPop.mTip)) {
                        mBean.departmentNo = data.comNo;
                        break;
                    }
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}